package com.ruoyi.video.service.impl;

import java.util.List;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.SnowUtil;
import com.ruoyi.video.dto.InitTaskDTO;
import com.ruoyi.video.mapper.SliceMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.video.mapper.UploadTaskMapper;
import com.ruoyi.video.domain.UploadTask;
import com.ruoyi.video.service.IUploadTaskService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * videoService业务层处理
 * 
 * @author Larry
 * @date 2024-03-22
 */
@Service
public class UploadTaskServiceImpl implements IUploadTaskService

{
    @Autowired
    private UploadTaskMapper uploadTaskMapper;
    @Resource
    private SliceMapper sliceMapper;

    /**
     * 查询video
     * 
     * @param id video主键
     * @return video
     */
    @Override
    public UploadTask selectUploadTaskById(Long id)
    {
        return uploadTaskMapper.selectUploadTaskById(id);
    }

    /**
     * 查询video列表
     * 
     * @param uploadTask video
     * @return video
     */
    @Override
    public List<UploadTask> selectUploadTaskList(UploadTask uploadTask)
    {
        return uploadTaskMapper.selectUploadTaskList(uploadTask);
    }

    /**
     * 新增video
     * 
     * @param uploadTask video
     * @return 结果
     */
    @Override
    public int insertUploadTask(UploadTask uploadTask)
    {
        return uploadTaskMapper.insertUploadTask(uploadTask);
    }

    /**
     * 修改video
     * 
     * @param uploadTask video
     * @return 结果
     */
    @Override
    public int updateUploadTask(UploadTask uploadTask)
    {
        return uploadTaskMapper.updateUploadTask(uploadTask);
    }

    /**
     * 批量删除video
     * 
     * @param ids 需要删除的video主键
     * @return 结果
     */
    @Override
    public int deleteUploadTaskByIds(Long[] ids)
    {
        return uploadTaskMapper.deleteUploadTaskByIds(ids);
    }

    /**
     * 删除video信息
     * 
     * @param id video主键
     * @return 结果
     */
    @Override
    public int deleteUploadTaskById(Long id)
    {
        return uploadTaskMapper.deleteUploadTaskById(id);
    }
   //实现秒传
    @Override
    public R<String> check(String md5) {
        UploadTask uploadTask = uploadTaskMapper.getByMd5(md5);
        if(uploadTask!=null){
            if(uploadTask.getFinish() == 1){
                return R.ok(uploadTask.getFileUrl());
            }
        }
        return R.ok();
    }

    @Override
    public R<Integer> getSlice(String md5) {
        UploadTask uploadTask = uploadTaskMapper.getByMd5(md5);
        if(uploadTask!=null){
            System.out.println(uploadTask.getCurrent());
            return R.ok(uploadTask.getCurrent());
        }
        return R.ok();
    }

    @Override
    public R<String> uploadSlice(String md5, Integer now, MultipartFile file) {
        uploadTaskMapper.updateByMd5(now,md5);
        return R.ok();
    }

    @Override
    public R<String> initTask(InitTaskDTO initTaskDTO) {
        UploadTask uploadTask = new UploadTask();
        BeanUtils.copyProperties(initTaskDTO,uploadTask);
        uploadTask.setBucketName("test");
        uploadTask.setChunkNum(initTaskDTO.getChunkNum());
        uploadTask.setId(SnowUtil.getSnowflakeNextId());
        uploadTask.setCurrent(0);
        uploadTaskMapper.insertOne(uploadTask);
        return R.ok();
    }
}
